package com.rndemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.react.RNActivity;
import com.facebook.react.ReactRootView;
import com.facebook.react.bridge.Argument;
import com.facebook.react.bridge.WritableMap;
import com.rndemo.rn.api.RouteInterceptor;


/**
 * Created by Administrator on 2018/4/14.
 * 这里是为了可以将rn 当作view 内嵌到界面中，儿不是整个页面都是rn 的
 * 基于 react-native-0.54.3
 */
public class BaseRNActivity extends RNActivity {
    @Override
    protected String getMainComponentName() {
        if(mDelegate==null){
            return super.getMainComponentName();
        }else{
            return mDelegate.getMainComponentName();
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        loadApp(null,getIntent().getExtras());
        rnAttacheToView(getWindow().peekDecorView());
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        if(!(view instanceof ReactRootView)){
            loadApp(null,getIntent().getExtras());
            rnAttacheToView(view);
        }
    }

    /**
     *  1.寻找并替换原view  中 R.id.reactRootView 的子view
     *  2.寻找上一次 ReactRootView 并替换
     *  3.将ReactRootView设置成根view
     */
    public void setContentView(String rnModule, Bundle bundle) {
        loadApp(rnModule,bundle);
        rnAttacheToView(getWindow().peekDecorView());
    }

    private void rnAttacheToView(View view){
        if (view instanceof ReactRootView){
            return;
        }
        if(getReactRootView()!=null){
            if(view!=null){
                view = view.findViewById(R.id.swipe_target);//这里将会替换 R.id.reactRootView（最多一个） 为 rn的view
                if(view==null){
                    view=mDelegate.getmProRootView();
                }
            }
            replaceView(view,getReactRootView());
        }
    }

    /**
     * 替换view
     * @param old
     * @param newView
     */
    private void replaceView(View old,View newView){
        if(old==null){
            if(newView!=null&&newView.getParent()==null){
                super.setContentView(newView);
            }
            return;
        }else if(old == newView){
            return;
        }
        ViewGroup parent = (ViewGroup) old.getParent();
        if(parent!=null){
            int i = parent.indexOfChild(old);
            parent.removeViewAt(i);
            if(newView!=null){
                if(newView.getParent()!=null){
                    ((ViewGroup)newView.getParent()).removeView(newView);
                }
                ViewGroup.LayoutParams lp = newView.getLayoutParams();
                if(lp == null){
                    lp = new ReactRootView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
                }
                newView.setLayoutParams(lp);
                parent.addView(newView,i);
            }
        }


    }
    //============================================================

    /**
     * 通知js 启动 activity
     */
    @Override
    public void startActivity(Intent intent) {
        try {
            RouteInterceptor RouteInterceptor = getReactInstanceManager().getCurrentReactContext().getJSModule(RouteInterceptor.class);
            WritableMap writableMap = Argument.fromBundle(intent.getExtras());
            writableMap.putString("activity",intent.getComponent().getClassName());
            RouteInterceptor.push(writableMap);
        }catch (Exception e){
            super.startActivity(intent);
        }
    }
    /**
     * 通知js 启动 activity
     */
    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        try {
            RouteInterceptor RouteInterceptor = getReactInstanceManager().getCurrentReactContext().getJSModule(RouteInterceptor.class);
            WritableMap writableMap = Argument.fromBundle(intent.getExtras());
            writableMap.putString("activity",intent.getComponent().getClassName());
            writableMap.putInt("requestCode",requestCode);
            RouteInterceptor.push(writableMap);
        }catch (Exception e){
            super.startActivityForResult(intent, requestCode);
        }
    }
}
